<template>
    <div>
        <el-page-header :title="`规格详情(${$route.query.propertyName})`" @back='$router.back()'></el-page-header>
        <el-divider content-position='left'>
            <el-popover
                ref='popover'
                placement="bottom-start"
                width="500"
                trigger="click"
                @hide='resetPopover'>
                <el-form label-width="120px">
                    <el-form-item label='规格值'>
                        <el-input v-model='createForm.key_value'></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type='primary' @click='createConfirm'>确认</el-button>
                    </el-form-item>
                </el-form>
                <el-button type='primary' slot='reference'>添加规格值</el-button>
            </el-popover>
            <!-- <el-button type='danger' v-if="history.length > 1" @click='backToParent'>返回上一级列表</el-button> -->
        </el-divider>
        <el-table
            :data="tableData"
            stripe
            border
            :header-cell-style="TABLE_HEADER_CELL_STYLE">
            <el-table-column label='id' prop="id" width="100"></el-table-column>
            <el-table-column prop='type_name' label='规格值'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='key_value' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column prop='sort' label='排序'>
                <template v-slot='scope'>
                    <edit-table-column v-model='scope.row' edit-key='sort' @change='updateHandle(scope.row)'></edit-table-column>
                </template>
            </el-table-column>
            <el-table-column label='操作'>
                <template v-slot='scope'>
                    <el-popconfirm title="确认删除吗？" @confirm='deleteHandle(scope.row)' class="popconfirm">
                        <el-button type='text' slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <o-pagination :total='total' @current-change='currentChange'></o-pagination>
    </div>
</template>

<script>
    import {toast, alert} from '@u'
    export default {
        data () {
            return {
                tableData : [],
                queryForm : {
                    page : 1,
                    limit : 10,
                    key_id : this.$route.params.id
                },
                total : 0,
                createForm : {
                    key_id : this.$route.params.id,
                    key_value : null,
                }
            }
        },
        created () {
            this.getTableData();
        },
        methods : {
            getTableData () {
                this.__api.commodity.specsValueList(this.queryForm).then(res => {
                    this.isLoading = false;
                    this.total = res.total;
                    this.tableData = res.data;
                })
            },
            //删除
            deleteHandle (row) {
                this.__api.commodity.specsValueDelete({
                    id : row.id
                }).then(_ => {
                    toast('操作成功');
                    this.getTableData();
                })
            },
            //更新
            updateHandle (row) {
                this.$nextTick(_ => {
                    this.__api.commodity.specsValueUpdate(row).then(_ => {
                        toast('操作成功')
                    })
                })
            },
            //创建确认
            createConfirm () {
                if (!this.createForm.key_value) {
                    toast('请填写规格值', 'error');
                    return;
                }
                this.__api.commodity.specsValueCreate(this.createForm).then(_ => {
                    toast('操作成功');
                    this.$refs.popover.doClose();
                    this.getTableData();
                    this.resetPopover();
                })
            },
            currentChange (page) {
                this.queryForm.page = page;
                this.getTableData();
            },
            //重置相关数据
            resetPopover () {
                this.createForm = {
                    key_name : null
                }
            },
            toDetail (row) {
                this.$router.push(`/commodity/property-detail/${row.id}`)
            }
        }
    }
</script>

<style>

</style>